package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.OrganizationMapper;
import cc.rengu.igas.bomp.common.entity.ExportMchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.Organization;
import cc.rengu.igas.bomp.common.enums.WechatActivityStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 * Updated by zhangxuran on 2020/03/23  新增支付宝特殊费率商户查询条件过滤方法
 */
public class MchntBaseInfoMapperImpl implements MchntBaseInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntBaseInfo(MchntBaseInfo mchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntBaseInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntBaseInfoById(MchntBaseInfo mchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntBaseInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户基本信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), mchntBaseInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoList() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoList");
        List<Object> list=dbsUtil.dbsFetchListData(sqlStr,null,MchntBaseInfo.class);
        return  list.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());

    }


    @Override
    public MchntBaseInfo selectMchntBaseInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoById");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntBaseInfo;
    }

    @Override
    public MchntBaseInfo selectMchntBaseInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByPrimaryKey");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBaseInfo, mchntBaseInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntBaseInfo;
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByMchntNoOrMchntName(String instId, String mchntNo, String mchntName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByMchntNoOrMchntName");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntBaseInfo.setMchntNo("%" + mchntNo + "%");
        }
        if (null != mchntName && !mchntName.isEmpty()) {
            mchntBaseInfo.setMchntName("%" + mchntName + "%");
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号或者商户名称模糊查询商户基本信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>", instId, mchntNo, mchntName);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByChainMchntNo(String instId, String chainMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByChainMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setChainMchntNo(chainMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据连锁商户号查询商户基本信息失败,instId:<{}>,chainMchntNo:<{}>", instId, chainMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByPlatMchntNo(String instId, String platMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByPlatMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setPlatMchntNo(platMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据平台商户号查询商户基本信息失败,instId:<{}>,platMchntNo:<{}>", instId, platMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByGroupMchntNo(String instId, String groupMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByGroupMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setGroupMchntNo(groupMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据集团商户号查询商户基本信息失败,instId:<{}>,groupMchntNo:<{}>", instId, groupMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByAgentMchntNo(String instId, String agentMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByAgentMchntNo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setAgentMchntNo(agentMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据代理商商户号查询商户基本信息失败,instId:<{}>,agentMchntNo:<{}>", instId, agentMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoByManagerId(String instId, String managerId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoByManagerId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setManagerId(managerId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据拓展经理查询商户基本信息失败,instId:<{}>,managerId:<{}>", instId, managerId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectMchntBaseInfoBySignInstId(String instId, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntBaseInfoBySignInstId");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        mchntBaseInfo.setSignInstId(signInstId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户签约机构查询商户基本信息失败,instId:<{}>,signInstId:<{}>", instId, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntBaseInfo> selectBompMchntBaseInfoByMchntInfo(String instId, String mchntNo, String mchntName, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByMchntInfo");
        MchntBaseInfo mchntBaseInfo = new MchntBaseInfo();
        mchntBaseInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            mchntBaseInfo.setMchntNo("%" + mchntNo + "%");
        }
        if (null != mchntName && !mchntName.isEmpty()) {
            mchntBaseInfo.setMchntName("%" + mchntName + "%");
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            mchntBaseInfo.setSignInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号或者商户名称模糊查询商户基本信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>,signInstId:<{}>", instId, mchntNo, mchntName, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBompMchntBaseInfoByMchntInfoFilter(String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByMchntInfo");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, ExportMchntBaseInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据商户号或者商户名称模糊查询商户基本信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>,signInstId:<{}>", instId, mchntBaseInfo.getMchntNo(), mchntBaseInfo.getMchntName(), mchntBaseInfo.getSignInstId());
            return null;
        }
        return pageResult;
    }

    @Override
    public PageResult selectBompMchntBaseInfoByInspectTask(String instId, MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByInspectTask");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据巡检任务条件查询商户基本信息失败");
            return null;
        }
        return pageResult;
    }

    @Override
    public PageResult selectBompMchntBaseInfo(MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntNo())) {
            mchntBaseInfo.setMchntNo("%" + mchntBaseInfo.getMchntNo() + "%");
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfo");

        return dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
    }

    @Override
    public PageResult selectBompMchntBaseInfoBySignInstId(MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntNo())) {
            mchntBaseInfo.setMchntNo("%" + mchntBaseInfo.getMchntNo() + "%");
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoBySignInstId");

        return dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
    }

    @Override
    public List<MchntBaseInfo> selectBompMchntBaseInfoByInspectTasks(String instId, MchntBaseInfo mchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByInspectTask");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据巡检任务条件查询商户基本信息失败");
            return null;
        }
        return objectList.stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<Object> selectMchntBaseInfoByInspectTask(String instId, MchntBaseInfo mchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByInspectTask");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据巡检任务条件查询商户基本信息失败");
            return null;
        }
        return objectList;
    }

    @Override
    public PageResult selectBompMchntBaseInfoByAuditStatus(MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        //1-全部待审核，2-审核通过，3-审核拒绝，5-全部已审核
        if ("5".equals(mchntBaseInfo.getAuditStatus())) {
            //不为草稿和待审核的数据
            mchntBaseInfo.setRemark3("%1");
            mchntBaseInfo.setRemark4("%0");
            mchntBaseInfo.setAuditStatus(null);
        } else {
            String auditStatus = "%" + mchntBaseInfo.getAuditStatus();
            mchntBaseInfo.setAuditStatus(auditStatus);
        }

        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntNo())) {
            mchntBaseInfo.setMchntNo("%" + mchntBaseInfo.getMchntNo() + "%");
        }

        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBaseInfoByAuditStatus");

        return dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
    }

    @Override
    public List<Object> selectBompTxnStaticByInspectTask(MchntBaseInfo mchntBaseInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTxnStaticByInspectTask");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据巡检任务条件查询交易量偏低商户基本信息失败");
            return null;
        }
        return objectList;
    }

    @Override
    public PageResult selectBompTxnStaticByInspectTask(MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTxnStaticByInspectTask");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据巡检任务条件查询交易量偏低商户基本信息失败");
            return null;
        }
        return pageResult;
    }

    @Override
    public PageResult selectWechatActivityMchntBaseInfo(MchntBaseInfo mchntBaseInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }

        // 初始化SQL语句
        String sqlStr;
        String wechatParamVal = mchntBaseInfo.getRemark1();
        // 商户名称模糊查询
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntName())) {
            mchntBaseInfo.setMchntName("%" + mchntBaseInfo.getMchntName() + "%");
        }
        // 如果活动申请结果不为空, 且活动申请结果为I-审核中 Y-审核通过 R-审核驳回
        // 则查询 T_B_MCHNT_BASE_INFO(商户基本信息表) T_B_WECHAT_MCHNT_INFO(微信商户信息表) T_B_MCHNT_SYS_PARAM(商户拓展属性表)
        if((!StringUtil.isEmptyOrNull(wechatParamVal))
                && (wechatParamVal.equals(WechatActivityStatusEnum.APPLICATION_STATE_AUDITING.getEpayStatus())
                || wechatParamVal.equals(WechatActivityStatusEnum.APPLICATION_STATE_PASSED.getEpayStatus())
                || wechatParamVal.equals(WechatActivityStatusEnum.APPLICATION_STATE_REJECTED.getEpayStatus()))) {
            // 连接查询 T_B_MCHNT_SYS_PARAM(商户拓展属性表)
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectWechatActivityMchntSysParam");

        } else if(!(StringUtil.isEmptyOrNull(wechatParamVal)) && "N".equals(wechatParamVal)) {
            // 如果活动申请结果不为空-且活动申请结果为 N-未申请
            // 则查询 T_B_MCHNT_BASE_INFO(商户基本信息表) 和 T_B_WECHAT_MCHNT_INFO(微信商户信息表)
            // 并将活动申请结果为I-审核中 Y-审核通过 R-审核驳回 的商户排除
            sqlStr =  (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectWechatActivity");
            sqlStr += "\n"+"AND WEC.MCHNT_NO NOT IN (SELECT PROPERTY_KEY FROM T_B_MCHNT_APEX_INFO " +"\n"+
                    "WHERE PROPERTY_KEY ='wActivityStatus' AND (PROPERTY_VALUE='I' OR PROPERTY_VALUE='Y' OR PROPERTY_VALUE='R'))" + "\n";

        } else {
            // 如果活动申请结果为空
            // 则查询 T_B_MCHNT_BASE_INFO(商户基本信息表) 和 T_B_WECHAT_MCHNT_INFO(微信商户信息表)
            sqlStr =  (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectWechatActivity");

        }

        if (!(StringUtil.isEmptyOrNull(mchntBaseInfo.getSignInstId()) || mchntBaseInfo.getSignInstId().endsWith("000000"))) {
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            List<Organization> organizationList = organizationMapper.selectOrgInfoByorgCode(mchntBaseInfo.getInstId(), mchntBaseInfo.getSignInstId());
            StringBuffer organList = new StringBuffer();
            if (!CollectionUtils.isEmpty(organizationList)) {
                for (Organization organization : organizationList) {
                    organList.append("'" + organization.getOrgCode() + "',");
                }
            }
            sqlStr += "\n" + "AND BAS.SIGN_INST_ID IN (" + organList.toString().substring(0, organList.toString().length() - 1) + ")" + "\n";
        }
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntNature())) {
            sqlStr += "\n" + "AND (BAS.MCHNT_NATURE || '-' || BAS.INDUSTRY_TYPE) IN (" + mchntBaseInfo.getMchntNature() + ")" + "\n";
        }
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntName())) {
            sqlStr += "\n" + "AND  (BAS.MCHNT_NAME like $mchntName OR BAS.MCHNT_NO like $mchntName)" + "\n";
        }
        // 设置 T_B_MCHNT_BASE_INFO(商户基本信息表) 中 商户状态(MCHNT_STATUS) 的查询条件
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntStatus())) {
            sqlStr += "\n" + "AND  (BAS.MCHNT_STATUS = " + "'"+ mchntBaseInfo.getMchntStatus() + "'" +")" + "\n";
        }

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, mchntBaseInfo, MchntBaseInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("查询微信特殊费率活动商户列表失败");
            return null;
        }
        return pageResult;
    }
}
